# coding=utf-8
# 编译日期：2022-09-15 19:40:15
# 版权所有：www.i-search.com.cn
import shutil
import openpyxl
import datetime 
import ubpa.init_input as iinput
import time
import pdb
from ubpa.ilog import ILog
from ubpa.base_img import set_img_res_path
import getopt
from sys import argv
import sys
import os
from base_img import *

class RpaAsiM:
     
    def __init__(self,**kwargs):
        self.__logger = ILog(__file__)
        self.path = set_img_res_path(__file__)
        self.robot_no = ''
        self.proc_no = ''
        self.job_no = ''
        self.input_arg = ''
        if('robot_no' in kwargs.keys()):
            self.robot_no = kwargs['robot_no']
        if('proc_no' in kwargs.keys()):
            self.proc_no = kwargs['proc_no']
        if('job_no' in kwargs.keys()):
            self.job_no = kwargs['job_no']
        if('input_arg' in kwargs.keys()):
            self.input_arg = kwargs['input_arg']
            if(len(self.input_arg) <= 0):
                self.input_arg = iinput.load_init(__file__)
            if self.input_arg is None:
                sys.exit(0)
    def Main(self):
        def procTem(s):
            '''
            格式化字符串
            '''
            return s.replace('℃','')
        
        
        ### 计算最高、最低温度和时间
        maxTemp = [lv_table1.iloc[2][1],lv_table1.iloc[0][1]]
        minTemp = [lv_table1.iloc[2][1],lv_table1.iloc[0][1]]
        
        for index in lv_table1.iloc[0].index:
            if index == 0:
                continue
            if procTem(lv_table1.iloc[2][index]) > procTem(maxTemp[0]):
                maxTemp = [lv_table1.iloc[2][index],lv_table1.iloc[0][index]]
            if procTem(lv_table1.iloc[2][index]) < procTem(maxTemp[0]):
                minTemp = [lv_table1.iloc[2][index],lv_table1.iloc[0][index]]
        
        print(maxTemp)
        print(minTemp)
        
        ## 时间
        dateTnow = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
        datenow = datetime.datetime.now().strftime("%Y-%m-%d ")
        datenow = datetime.datetime.now()..strftime("%Y-%m-%d ")
        nowH = datetime.datetime.now().hour
        
        if int(maxTemp[1].split(:)[0]) < nowH:
            maxTemp[1] 
        
        ### 生成目标excel
        
        source_file = r'C:\Users\wangffeing\Desktop\RPAAS\0 需求\城市天气信息获取-XX城市.xlsx'
        target_file = r'C:\Users\wangffeing\Desktop\RPAAS\0 需求\城市天气信息获取-{0}.xlsx'.format(cityname)
        
        try:
        #    shutil.copy(source_file, target_file)+
           
           
            wb = openpyxl.load_workbook(source_file)
            sh = wb['天气信息']
            sh['A2'] = cityname
            sh['B2'] = dateTnow
            sh['C2'] = lv_temperature
            sh['D2'] = lv_rain
            sh['E2'] = lv_wind
            sh['F2'] = maxTemp[0]
            sh['G2'] = maxTemp[1]
            sh['H2'] = minTemp[0]
            sh['I2'] = minTemp[1]
            wb.save(target_file)
           
        except IOError as e:
            print("无法生成天气excel文件 %s" % e)
        except:
           print("错误:", sys.exc_info())
           
           
           
           
 
if __name__ == '__main__':
    ILog.begin_init()
    robot_no = ''
    proc_no = ''
    job_no = ''
    input_arg = ''
    try:
        argv = sys.argv[1:]
        opts, args = getopt.getopt(argv,"hr:p:j:i:",["robot = ","proc = ","job = ","input = "])
    except getopt.GetoptError:
        print ('robot.py -r <robot> -p <proc> -j <job>')
    for opt, arg in opts:
        if opt == '-h':
            print ('robot.py -r <robot> -p <proc> -j <job>')
        elif opt in ("-r", "--robot"):
            robot_no = arg
        elif opt in ("-p", "--proc"):
            proc_no = arg
        elif opt in ("-j", "--job"):
            job_no = arg
        elif opt in ("-i", "--input"):
            input_arg = arg
    pro = RpaAsiM(robot_no=robot_no,proc_no=proc_no,job_no=job_no,input_arg=input_arg)
    pro.Main()
